package com.warm.pump.module.work.service.impl;

import com.warm.pump.executor.InstanceService;
import com.warm.pump.module.skd.dao.gen.SkdBoDao;
import com.warm.pump.module.work.bean.WorkTree;
import com.warm.pump.module.work.bean.po.gen.WorkItem;
import com.warm.pump.module.work.bean.po.gen.WorkSubject;
import com.warm.pump.module.work.dao.gen.WorkSubjectDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import com.warm.pump.module.work.dao.gen.WorkDao;
import com.warm.pump.module.work.bean.po.gen.Work;
import com.warm.pump.module.work.service.IWorkService;
import com.warm.pump.frame.gen.bean.PageBean;
import org.springframework.transaction.annotation.Transactional;


@Service("WorkService")
public class WorkServiceImpl implements IWorkService {
	@Autowired
	private WorkDao workDao;

	@Autowired
	private WorkSubjectDao workSubjectDao;

	@Autowired
	private SkdBoDao skdBoDao;
	@Autowired
	private InstanceService instanceService;

	@Override
	public Work queryObject(Long  id){
		return workDao.selectByPrimaryKey( id);
	}

	public Work queryObject(Map<String, Object> map){
		return workDao.selectByMap( map);
	}
	
	@Override
	public List<Work> queryList(Map<String, Object> map){
		return workDao.getListByMap(map);
	}

	@Override
	public PageBean getPageBeanByMap(Map map, int pageNo, int pageSize, Long jobId) {
		if (null != jobId) {
//			Map<String,Object> skdMap = new HashMap<>();
//			skdMap.put("jobId",jobId);
//			List<Long> subjectIds = skdBoDao.selectSubjectIdByJobId(skdMap);
//			if(null != subjectIds && subjectIds.size()>0){
//				map.put("subjectId_notIn",subjectIds);
//			}
//
//			List<Long> itemIds = skdBoDao.selectItemIdByJobId(skdMap);
//			if(null != itemIds&&item  Ids.size()>0){
//				map.put("id_notIn",itemIds);
//			}
		}
		return workDao.getPageBeanByMap(map, pageNo, pageSize);
	}

	/**
	 * 获取作业树
	 *
	 * @return
	 */
	public List<WorkTree> getWorkTree() {
		List<WorkTree> treeList = new ArrayList<>();
		//1.获取专题
		Map<String, Object> subJectMap = new HashMap<>();
		subJectMap.put("orderBy", "order_num desc");
		List<WorkSubject> subjectList = workSubjectDao.getListByMap(subJectMap);


		for (WorkSubject subject : subjectList) {
			WorkTree itemTree = new WorkTree();
			itemTree.setId("workSubject_" + subject.getId());
			itemTree.setName(subject.getSubjectName());
			itemTree.setParentId("0");
			itemTree.setType("workSubject");
			itemTree.setOrderNum(0);
			itemTree.setLevel(1);
			treeList.add(itemTree);
			//2.通过专题获取 作业
			Map<String, Object> workMap = new HashMap<>();
			workMap.put("subjectId",subject.getId());
			workMap.put("orderBy", "order_num desc");
			List<Work> workList = workDao.getListByMap(workMap);
			if(null != workList && workList.size()>0){
				for (Work work :workList){
					itemTree = new WorkTree();
					itemTree.setId("work_" + work.getId());
					itemTree.setName(work.getWorkName());
					itemTree.setParentId("workSubject_" + subject.getId());
					itemTree.setType("work");
					itemTree.setOrderNum(0);
					itemTree.setLevel(2);
					treeList.add(itemTree);
				}
			}

		}

		//3处理没有归属专题的作业
		WorkTree subjectTree = new WorkTree();
		subjectTree.setId("workSubject_" + 9999999999l);
		subjectTree.setName("无归属专题");
		subjectTree.setParentId("0");
		subjectTree.setType("workSubject");
		subjectTree.setOrderNum(0);
		subjectTree.setLevel(1);
		treeList.add(subjectTree);
		Map<String, Object> itemMap = new HashMap<>();
		itemMap.put("subjectId_null",1);
		itemMap.put("orderBy", "order_num desc");
		List<Work> workList = workDao.getListByMap(itemMap);

		for (Work work :workList){
			WorkTree itemTree = new WorkTree();
			itemTree.setId("work_" + work.getId());
			itemTree.setName(work.getWorkName());
			itemTree.setParentId("workSubject_" + 9999999999l);
			itemTree.setType("work");
			itemTree.setOrderNum(0);
			itemTree.setLevel(2);
			treeList.add(itemTree);
		}

		return treeList;
	}

	/**
	 * 通过调度任务Id 获取 作业专题 作业项 tree
	 *
	 * @param map
	 * @param jobId
	 * @return
	 */
	public List<WorkTree> getWorkTreeByJobIdList(Map map, Long jobId) {
		if (null != jobId) {
			Map<String, Object> skdMap = new HashMap<>();
			skdMap.put("jobId", jobId);
			List<Long> itemIds = skdBoDao.selectWorkIdByJobId(skdMap);
			if (null != itemIds && itemIds.size() > 0) {
				map.put("id_notIn", itemIds);
			}
		}
		map.put("status", "1");
		map.put("orderBy", "subject_id");
		List<Work> itemList = workDao.getListByMap(map);
		Map<Long, Long> subjectMap = new HashMap<>();
		List<WorkTree> treeList = new ArrayList<>();
		for (Work item : itemList) {
			Long subjectId = item.getSubjectId();
			if (null == subjectId) {
				subjectId = 9999999999l;
			}
			if (!subjectMap.containsKey(subjectId)) {
				if (9999999999l == subjectId) {
					WorkTree subjectTree = new WorkTree();
					subjectTree.setId("workSubject_" + 9999999999l);
					subjectTree.setName("无归属专题");
					subjectTree.setParentId("0");
					subjectTree.setType("workSubject");
					subjectTree.setOrderNum(0);
					subjectTree.setLevel(1);
					treeList.add(subjectTree);
					subjectMap.put(9999999999l, 9999999999l);
				} else {
					WorkSubject subject = workSubjectDao.selectByPrimaryKey(item.getSubjectId());
					if (null != subject) {
						WorkTree subjectTree = new WorkTree();
						subjectTree.setId("workSubject_" + subject.getId());
						subjectTree.setName(subject.getSubjectName());
						subjectTree.setParentId("0");
						subjectTree.setType("workSubject");
						subjectTree.setOrderNum(0);
						subjectTree.setLevel(1);
						treeList.add(subjectTree);
						subjectMap.put(subject.getId(), subject.getId());
					}
				}
			}

			WorkTree tree = new WorkTree();
			tree.setId("work_" + item.getId());
			tree.setName(item.getWorkName());
			tree.setParentId("workSubject_" + subjectId);
			tree.setType("work");
			tree.setOrderNum(0);
			tree.setLevel(2);
			treeList.add(tree);
		}
		return treeList;
	}
	@Override
	public int queryTotal(Map<String, Object> map){
		return workDao.countTotalByMap(map);
	}
	
	@Override
	public void save(Work work){
		workDao.insertSelective(work);
	}
	
	@Override
	public void update(Work work){
		workDao.updateByPrimaryKeySelective(work);
	}
	public void updateKeySelective(Work work){
		workDao.updateByPrimaryKeySelective(work);
	}
	@Override
	public void delete(Long  id){
		workDao.deleteByPrimaryKey(id);
	}

	@Override
	public void delete(Map<String, Object>  map){
	workDao.deleteByMap(map);
	}
	
	@Override
	public void deleteBatch(Long[]  ids){
		Map<String, Object> map = new HashMap<>();
		map.put("id_in",ids);
		workDao.deleteByMap(map);
	}

	/**
	 * 作业管理 立即执行
	 * @param ids
	 */
	@Override
	public void run(Long[] ids) {
		for (Long id : ids) {
			this.createInstByWork(id);
		}
	}

	/**
	 * 通过作业创建实例
	 *
	 * @param workId
	 */
	@Transactional
	public void createInstByWork(Long workId) {
		this.createInstByWork(workId, "work", "作业列表中立即执行");
	}

	/**
	 * 通过作业创建实例
	 *
	 * @param workId
	 * @param referType
	 * @param remark
	 */
	public void createInstByWork(Long workId, String referType, String remark) {
		Work work = workDao.selectByPrimaryKey(workId);
		//创建调度实例
		String instId = instanceService.createInst(referType, workId, work.getWorkName(), 1, remark);
		//创建实例明细
		instanceService.createInstDtl(instId, workId, work.getWorkName(), null, null, null);
	}
}
